package sketchpad.gui.control;

import sketchpad.constant.Config;
import sketchpad.constant.Geometry;
import sketchpad.gui.drawinggraph.*;
import sketchpad.gui.drawinggraph.node.Node;

//本类负责生产各个元素
public class ElementFactory {

    private ElementFactory(){}

    private static ElementFactory elementFactory = new ElementFactory();

    private static DrawControl control;

    public static ElementFactory getInstance(DrawControl drawControl){
        control = drawControl;
        return elementFactory;
    }

    public DrawItem create(Geometry geometry, Node node){
        DrawItem drawItem;
        int id = control.getGlobalId();
        boolean isSelect = control.isSelect();
        switch (geometry){
            case RECTANGLE:
                drawItem = new Rectangle(id, node);
                if (isSelect){
                    drawItem.setId(Config.SELECT_ID);
                    drawItem.setSelect(true);
                }
                break;
            case CIRCLE:
                drawItem = new Circle(id, node);
                break;
            case TRIANGLE:
                drawItem = new Triangle(id, node);
                break;
            case RANDOM:
                drawItem = new Random(id, node);
                break;
            default:
                drawItem = null;
        }
        control.getDrawItemMap().put(drawItem==null?id:drawItem.getId(),drawItem);
        return drawItem;
    }
}
